<template>
  <el-dialog
    v-el-drag-dialog
    title="客户注销"
    width="450px"
    :visible.sync="visible"
    append-to-body
    @closed="reset"
  >
    <el-form ref="dataForm" label-position="right" label-width="80px">
      <el-form-item label="销户原因:" prop="remark">
        <el-input v-model="remark" type="textarea" :rows="2" maxlength="100" show-word-limit />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="reset">取 消</el-button>
      <el-button type="primary" @click="success">确 定</el-button>
      <el-button type="primary" @click="tryPrint">打 印</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { parseTime } from '@/utils'
  import { csmCancelPrint } from '@/api'
  export default {
    name: 'xiaohu',
    props: {
      xiaoHuFn: {
        type: Function
      }
    },
    data() {
      return {
        visible: false,
        remark: ''
      }
    },
    methods: {
      open(temp, row) {
        this.query = { ...temp }
        this.row = row
        this.visible = true
      },
      success() {
        // invalidStr
        const params = { ...this.query, invalidStr: this.remark }
        // console.log(this.$parents)
        console.log(this.xiaoHuFn)
        this.xiaoHuFn(params)
        this.remark = ''
        this.visible = false
      },
      async tryPrint() {
        const { status, data } = await csmCancelPrint({ csmId: this.row.csmId })
        if (status === 200) {
          this.print(this.row, data)
        }
      },
      print(row2, resdata) {
        const row = JSON.parse(JSON.stringify(row2))
        const cache = JSON.parse(localStorage.tp)
        if (!cache) {
          this.$message.warning('请先设置打印模板')
          return
        }
        const temp = cache['31']
        if (!temp) {
          this.$message.warning('请先设置打印模板')
          return
        }
        if (row.cancelTime) {
          row.cancelDt = parseTime(row.cancelTime, '{y}-{m}-{d} {h}:{i}:{s}')
        } else {
          row.cancelDt = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
        }
        row.csmAddress = row.csmRgAddress.replace(
          /([\u4E00-\u9FA5]*)([省市])(\s*)([\u4E00-\u9FA5]*)市/g,
          ''
        )
        row.invalidStr = this.remark
        let printcmd = {
          cmd: 2,
          title: `正在对销户单${row.csmCode}进行打印`,
          elements: []
        }
        printcmd = { ...printcmd, ...temp }
        if (!window.jCPrint || !window.jCPrint.ws) {
          this.$message.error('打印未连接')
          return
        }
        window.jCPrint.process(printcmd, temp, row, resdata)
        if (window.jCPrint.firePush(JSON.stringify(printcmd))) {
          this.$message.success('打印指令已发送')
        } else {
          this.$message.error('打印未连接')
        }
      },
      reset() {
        this.visible = false
        this.remark = ''
      }
    }
  }

</script>
